﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Jungle.Code.Core;
using System.Collections.Generic;
using Jungle.Code.BLL;
using Jungle.Code.Utils;

namespace Jungle.Admin
{
    public partial class ModulesAdmin : System.Web.UI.Page
    {
        string mod = "";

        public void Page_PreInit()
        {
            this.Theme = "Admin";

        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request["mod"] != null)
                mod = Request["mod"];
            Display();
        }


        #region Display functions
        private void Display()
        {
            FillModulesList();
        }
        private void FillModulesList()
        {
            List<Module> list = new List<Module>();
            list = Module.GetAll();
            foreach (Module m in list)
            {
                if (m.Group == 4)
                {
                    Control uc = base.LoadControl(UrlHelper.GetApplicationPath() + m.BasePath + m.AdminControlFile);
                    BaseAdminModule bam = (BaseAdminModule)uc;
                    bam.FillBaseInfo();

                    HtmlGenericControl cntr = new HtmlGenericControl("div");
                    HyperLink hl = new HyperLink();
                    hl.Text = m.Name;
                    hl.NavigateUrl = FormatUrl("Modules.aspx", bam.sysName, "default", "");
                    cntr.Controls.Add(hl);
                    if (mod == bam.sysName)
                    {
                        hl.CssClass = "l2a";
                        bam.Main();
                        foreach (Tab t in bam.menu)
                        {
                            cntr.Controls.Add(GetLink(t.Name, FormatUrl("Modules.aspx", bam.sysName, t.Command, ""), 3, t.Active));
                        }
                        pnlMain.Controls.Add(uc);
                    }
                    else
                        hl.CssClass = "l2";

                    pnlModulesList.Controls.Add(cntr);

                }
            }

        }
        public Control GetLink(string text, string url, int level, bool active)
        {
            HtmlGenericControl cntr = new HtmlGenericControl("div");
            HyperLink hl = new HyperLink();
            hl.Text = text;
            hl.NavigateUrl = url;
            if (active)
                hl.CssClass = "l" + level.ToString() + "a";
            else
                hl.CssClass = "l" + level.ToString();
            cntr.Controls.Add(hl);
            return cntr;
        }
        public string FormatUrl(string page, string mod, string cmd, string args)
        {
            string str = String.Format("{0}{1}?mod={2}&cmd={3}", UrlHelper.GetAdminUrl(), page, mod, cmd);
            return str;
        }
        #endregion
    }
}
